﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrm.Services.Realization;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;

namespace FangChanCrm.Controllers
{
    public class HomeController : Controller, IHostedService
    {
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_BuMenBiaoService t_BuMenBiaoService;
        private readonly It_GangWeiBiaoService t_GangWeiBiaoService;
        private readonly It_GongSiBiaoService t_GongSiBiaoService;
        private readonly It_ZhanDuiBiaoService t_ZhanDuiBiaoService;
        private readonly It_ZhanQuBiaoService t_ZhanQuBiaoService;
        private readonly It_FangYuanXinXiBiaoService t_FangYuanXinXiBiaoService;
        private readonly It_XiaoXiTiXingBiaoService t_XiaoXiTiXingBiaoService;
        private readonly It_XiaoShouBiaoService t_XiaoShouBiaoService;
        private readonly It_XiaoShouXiangXiBiaoService t_XiaoShouXiangXiBiaoService;
        private readonly It_TuanDuiBiaoService t_TuanDuiBiaoService;
        private readonly It_TuanDuiRenYuanGuanLianBiaoService t_TuanDuiRenYuanGuanLianBiaoService;

        public HomeController(It_RenYuanBiaoService renYuanBiaoService, It_BuMenBiaoService buMenBiaoService, It_GangWeiBiaoService gangWeiBiaoService, It_GongSiBiaoService gongSiBiaoService, It_ZhanDuiBiaoService zhanDuiBiaoService, It_ZhanQuBiaoService zhanQuBiaoService, It_FangYuanXinXiBiaoService fangYuanXinXiBiaoService, It_XiaoXiTiXingBiaoService xiaoXiTiXingBiaoService, It_XiaoShouBiaoService xiaoShouBiaoService, It_XiaoShouXiangXiBiaoService xiaoShouXiangXiBiaoService, It_TuanDuiBiaoService tuanDuiBiaoService, It_TuanDuiRenYuanGuanLianBiaoService tuanDuiRenYuanGuanLianBiaoService, IConfiguration configuration)
        {
            t_RenYuanBiaoService = renYuanBiaoService;
            t_BuMenBiaoService = buMenBiaoService;
            t_GangWeiBiaoService = gangWeiBiaoService;
            t_GongSiBiaoService = gongSiBiaoService;
            t_ZhanDuiBiaoService = zhanDuiBiaoService;
            t_ZhanQuBiaoService = zhanQuBiaoService;
            t_FangYuanXinXiBiaoService = fangYuanXinXiBiaoService;
            t_XiaoXiTiXingBiaoService = xiaoXiTiXingBiaoService;
            t_XiaoShouBiaoService = xiaoShouBiaoService;
            t_XiaoShouXiangXiBiaoService = xiaoShouXiangXiBiaoService;
            t_TuanDuiBiaoService = tuanDuiBiaoService;
            t_TuanDuiRenYuanGuanLianBiaoService = tuanDuiRenYuanGuanLianBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IActionResult List()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                CancellationToken token = new CancellationToken();
                StartAsync(token);
                return View();
            }
        }
        public IActionResult Index()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                t_FangYuanXinXiBiaoService.GetLouHao(1);
                return View();
            }

        }
        public IActionResult YuQing()
        {
            return View();
        }

        public IActionResult CreateAndModify()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        [HttpPost]
        public string TestSendMessage()
        {
            try
            {
                Maticsoft.Web.XiaoXiTiXing.SendMessage(Request.Form["Id"].ToString(), "测试", "");
                return "success";
            }
            catch (Exception)
            {
                return "error";
                //throw;
            }
        }
        public Task StartAsync(CancellationToken cancellationToken)
        {
            return ExecuteAsync(cancellationToken);
        }

        public Task StopAsync(CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
        protected async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                await new TaskFactory().StartNew(() =>
                {
                    try
                    {
                        var time = DateTime.Now.ToString("HH:mm");
                        if ("08:00" == time || "9:00" == time || "10:00" == time || "11:00" == time || "12:00" == time || "13:00" == time || "14:00" == time || "15:00" == time || "16:00" == time || "" == time || "17:00" == time || "18:00" == time)
                        {
                            using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                            {
                                sw.Write("执行了一次发送消息方法，时间=" + DateTime.Now + " " + "\n");
                            }
                            //SendMessage();
                        }
                        SendMessage();
                    }
                    catch (Exception)
                    {
                        using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                        {
                            sw.Write("方法出错，时间为" + DateTime.Now + "\n");
                        }
                        //throw;
                    }
                    Thread.Sleep(1 * 60000);
                });
            }
        }
        private void SendMessage()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var context = new MyContext(optionsBuilder.Options))
            {
                var listbaobei = context.t_XiaoXiTiXingBiao.Include(x => x.t_RenYuanBiao).Include(x => x.t_KeHuBiao).ToList().Where(x => x.ShengYuTiXingCiShu > 0 && x.TiXingLeiXing == "报备" && Convert.ToDateTime(x.TiXingShiJian) < DateTime.Now).ToList();
                var listgenjin = context.t_XiaoXiTiXingBiao.Include(x => x.t_RenYuanBiao).Include(x => x.t_KeHuBiao).ToList().Where(x => x.ShengYuTiXingCiShu > 0 && x.TiXingLeiXing == "跟进" && Convert.ToDateTime(x.TiXingShiJian) < DateTime.Now).ToList();
                for (int i = listbaobei.Count - 1; i >= 0; i--)
                {
                    if (listbaobei[i].t_RenYuanBiao == null)
                    {
                        listbaobei.Remove(listbaobei[i]);
                        break;
                    }
                    else
                    {
                        listbaobei[i].t_RenYuanBiao.t_XiaoXiTiXingBiaos = null;
                    }
                    if (listbaobei[i].t_KeHuBiao == null)
                    {
                        listbaobei.Remove(listbaobei[i]);
                        break;
                    }
                    else
                    {
                        listbaobei[i].t_KeHuBiao.t_RenYuanBiao = null;
                        listbaobei[i].t_KeHuBiao.t_XiaoXiTiXingBiaos = null;
                    }
                }
                listbaobei = listbaobei.Where(x => x.t_RenYuanBiao != null && x.t_KeHuBiao != null).ToList();
                for (int i = listgenjin.Count - 1; i >= 0; i--)
                {
                    if (listgenjin[i].t_RenYuanBiao == null)
                    {
                        listgenjin.Remove(listgenjin[i]);
                        break;
                    }
                    else
                    {
                        listgenjin[i].t_RenYuanBiao.t_XiaoXiTiXingBiaos = null;
                    }
                    if (listgenjin[i].t_KeHuBiao == null)
                    {
                        listgenjin.Remove(listgenjin[i]);
                        break;
                    }
                    else
                    {
                        listgenjin[i].t_KeHuBiao.t_RenYuanBiao = null;
                        listgenjin[i].t_KeHuBiao.t_XiaoXiTiXingBiaos = null;
                    }
                }
                listgenjin = listgenjin.Where(x => x.t_RenYuanBiao != null && x.t_KeHuBiao != null).ToList();
                foreach (var item in listbaobei)
                {
                    try
                    {
                        if (item.t_RenYuanBiao == null || item.t_KeHuBiao == null)
                        {
                            break;
                        }
                        Maticsoft.Web.XiaoXiTiXing.SendMessage(item.t_RenYuanBiao.BeiZhu1, item.t_KeHuBiao.KeHuXingMing + "该报备了!", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww500458ad942a211f&redirect_uri=http://web.xyufu.cn/PhoneKeHu/MyCustomerDetail?KeHuId=" + item.KeHuId + "&response_type=code&scope=snsapi_privateinfo&agentid=1000002&state=STATE#wechat_redirect");
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            var mess = _context.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == item.Id);
                            mess.ShengYuTiXingCiShu = mess.ShengYuTiXingCiShu - 1;
                            mess.TiXingShiJian = Convert.ToDateTime(mess.TiXingShiJian).AddHours(72).ToString();
                            if (_context.SaveChanges() == 0)
                            {
                                using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                                {
                                    sw.Write("修改数据错误，报备，消息提醒表id为" + item.Id + "\n");
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                        {
                            sw.Write("发送消息错误，报备，消息提醒表id为" + item.Id + "\n");
                        }
                        //throw;
                    }
                }
                foreach (var item in listgenjin)
                {
                    try
                    {
                        if (item.t_RenYuanBiao == null || item.t_KeHuBiao == null)
                        {
                            break;
                        }
                        Maticsoft.Web.XiaoXiTiXing.SendMessage(item.t_RenYuanBiao.BeiZhu1, item.t_KeHuBiao.KeHuXingMing + "该报备了!", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww500458ad942a211f&redirect_uri=http://web.xyufu.cn/PhoneKeHu/MyCustomerDetail?KeHuId=" + item.KeHuId + "&response_type=code&scope=snsapi_privateinfo&agentid=1000002&state=STATE#wechat_redirect");
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            var mess = _context.t_XiaoXiTiXingBiao.FirstOrDefault(x => x.Id == item.Id);
                            mess.ShengYuTiXingCiShu = mess.ShengYuTiXingCiShu - 1;
                            mess.TiXingShiJian = Convert.ToDateTime(mess.TiXingShiJian).AddHours(72).ToString();
                            if (_context.SaveChanges() == 0)
                            {
                                using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                                {
                                    sw.Write("修改数据错误，跟进，消息提醒表id为" + item.Id + "\n");
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                        {
                            sw.Write("发送消息错误，跟进，消息提醒表id为" + item.Id + "\n");
                        }
                        //throw;
                    }
                }
                var listbeiwanglutixing = context.t_BeiWangLu.Where(x => x.TiXingShiJian != null).ToList();
                foreach (var item in listbeiwanglutixing)
                {
                    try
                    {
                        Maticsoft.Web.XiaoXiTiXing.SendMessage(context.t_RenYuanBiao.FirstOrDefault(x => x.Id == item.RenYuanId).BeiZhu1, "备忘录提醒！\n标题：\n" + item.BiaoTi + "\n内容：\n" + item.NeiRong + "\n", "https://open.weixin.qq.com/connect/oauth2/authorize?appid=ww500458ad942a211f&redirect_uri=http://web.xyufu.cn/PhoneBeiWangLu/List&response_type=code&scope=snsapi_privateinfo&agentid=1000002&state=STATE#wechat_redirect");
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            var asa = _context.t_BeiWangLu.FirstOrDefault(x => x.Id == item.Id);
                            asa.TiXingShiJian = null;
                            _context.SaveChanges();
                        }
                    }
                    catch (Exception)
                    {
                        using (StreamWriter sw = new StreamWriter("wwwroot/ce.txt"))
                        {
                            sw.Write("发送消息错误，消息提醒表id为" + item.Id + "\n");
                        }
                        //throw;
                    }
                }
            }
        }
        public IActionResult YeJiTongJi()
        {
            return View();
        }
        public IActionResult FenXiBaoBiao()
        {   
            var starttimemonth = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
            var endtimemonth = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1);
            var starttimeyear = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
            var endtimeyear = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1);
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var kehuyear = _context.t_KeHuBiao.ToList().Where(x => Convert.ToDateTime(x.BeiZhu1) > starttimeyear && Convert.ToDateTime(x.BeiZhu1) < endtimeyear).ToList();
                var kehumonth = kehuyear.Where(x => Convert.ToDateTime(x.BeiZhu1) > starttimemonth && Convert.ToDateTime(x.BeiZhu1) < endtimemonth).ToList();
                ViewData["xinzengkehushubyyear"] = kehuyear.Count;
                ViewData["xinzengkehushubymonth"] = kehumonth.Count;
                var xiaoshouxiangxiyear = _context.t_XiaoShouXiangXiBiao.Include(x => x.t_XiaoShouBiao).ThenInclude(x => x.t_KeHuBiao).Where(x => x.XiaoShouDongZuo == "确认到访").ToList().Where(x => Convert.ToDateTime(x.t_XiaoShouBiao.t_KeHuBiao.BeiZhu1) > starttimeyear && Convert.ToDateTime(x.t_XiaoShouBiao.t_KeHuBiao.BeiZhu1) < endtimeyear).ToList();
                var xiaoshouxiangximonth = xiaoshouxiangxiyear.Where(x => Convert.ToDateTime(x.t_XiaoShouBiao.t_KeHuBiao.BeiZhu1) > starttimemonth && Convert.ToDateTime(x.t_XiaoShouBiao.t_KeHuBiao.BeiZhu1) < endtimemonth).ToList();
                ViewData["daikanshubyyear"] = xiaoshouxiangxiyear.Count;
                ViewData["daikanshubymonth"] = xiaoshouxiangximonth.Count;
                var xiaoshoushuyear = _context.t_FangYuanKeHuGuanLianBiao.ToList().Where(x => Convert.ToDateTime(x.BeiZhu2) > starttimeyear && Convert.ToDateTime(x.BeiZhu2) < endtimeyear && x.BeiZhu1 != "已退房").ToList();
                var xiaoshoushumonth = xiaoshoushuyear.Where(x => Convert.ToDateTime(x.BeiZhu2) > starttimemonth && Convert.ToDateTime(x.BeiZhu2) < endtimemonth && x.BeiZhu1 != "已退房").ToList();
                ViewData["chengjiaoshubymonth"] = xiaoshoushumonth.Count;
                ViewData["chengjiaoshubyyear"] = xiaoshoushuyear.Count;
                ViewData["retmoneystrbymonth"] = xiaoshoushumonth.Sum(x => Convert.ToInt32(x.ChengJiaoJinE));
                ViewData["retmoneystrbyyear"] = xiaoshoushuyear.Sum(x => Convert.ToInt32(x.ChengJiaoJinE));
            }
            return View();
        }

        [HttpPost]
        public string InitializationDataTable()
        {
            t_RenYuanBiao t_renYuanBiao = new t_RenYuanBiao();
            t_renYuanBiao.ZhanQuId = Convert.ToInt32(Request.Form["selZhanQu"]);
            t_renYuanBiao.XingMing = Request.Form["xingming"];
            t_renYuanBiao.ZhanDuiId = Convert.ToInt32(Request.Form["selZhanDui"]);
            t_renYuanBiao.BeiZhu8 = Request.Form["KaiShiShiJian"];
            t_renYuanBiao.BeiZhu9 = Request.Form["JieShuShiJian"];

            string json = t_RenYuanBiaoService.GetListWithFenYeYeJi(t_renYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            return json;
        }


        [HttpPost]
        public string InitializationDataTable1()
        {
            t_RenYuanBiao t_renYuanBiao = new t_RenYuanBiao();
            t_renYuanBiao.XingMing = Request.Form["XingMing"];
            t_renYuanBiao.BeiZhu10 = Request.Form["BuMenId"];
            t_renYuanBiao.BeiZhu8 = Request.Form["KaiShiShiJian"];
            t_renYuanBiao.BeiZhu9 = Request.Form["JieShuShiJian"];
            string json = t_RenYuanBiaoService.GetListWithFenYeYeJi1(t_renYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            return json;
        }


        #region 业务员/置业顾问   按   月/年查看销售额
        [HttpPost]
        public string ReturnXiaoShouE()
        {
            var retlist = new List<t_RenYuanBiao>();
            if (Request.Form["leixing"] == "yewuyuan")
            {
                if (Request.Form["shijian"] == "yue")
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                    var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                    var shuju = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao != null).ToList();
                    var listren = shuju.Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员").ToList();
                    for (int i = 0; i < listren.Count; i++)
                    {
                        listren[i].t_BeiWangLus = null;
                        listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                        listren[i].t_KeHuBiaos = null;
                        listren[i].t_KeHuZiYuanChis = null;
                        listren[i].t_RenYuanQuanXians = null;
                        listren[i].t_XiaoShouBiao_QueRenRens = null;
                        listren[i].t_XiaoShouBiao_YeWuYuans = null;
                        listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                        listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                        listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                        listren[i].t_XiaoXiTiXingBiaos = null;
                        listren[i].t_YiJianFanKuiBiaos = null;
                        listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                        var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                        listren[i].BeiZhu9 = "0";
                        foreach (var item in foreachlist)
                        {
                            if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                            {
                                if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                                {
                                    if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                    {
                                        listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                    }
                                    else
                                    {
                                        listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                    }
                                }
                            }
                        }
                        listren[i].BeiZhu10 = foreachlist.Count.ToString();
                    }
                    retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
                }
                else
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1));
                    var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                    var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员").ToList();
                    for (int i = 0; i < listren.Count; i++)
                    {
                        listren[i].t_BeiWangLus = null;
                        listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                        listren[i].t_KeHuBiaos = null;
                        listren[i].t_KeHuZiYuanChis = null;
                        listren[i].t_RenYuanQuanXians = null;
                        listren[i].t_XiaoShouBiao_QueRenRens = null;
                        listren[i].t_XiaoShouBiao_YeWuYuans = null;
                        listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                        listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                        listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                        listren[i].t_XiaoXiTiXingBiaos = null;
                        listren[i].t_YiJianFanKuiBiaos = null;
                        listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                        listren[i].BeiZhu9 = "0";
                        var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                        foreach (var item in foreachlist)
                        {
                            if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                            {
                                if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                                {
                                    if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                    {
                                        listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                    }
                                    else
                                    {
                                        listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                    }
                                }
                            }
                        }
                        listren[i].BeiZhu10 = foreachlist.Count.ToString();
                    }
                    retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
                }
            }
            else
            {
                if (Request.Form["shijian"] == "yue")
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                    var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                    var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
                    for (int i = 0; i < listren.Count; i++)
                    {
                        listren[i].t_BeiWangLus = null;
                        listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                        listren[i].t_KeHuBiaos = null;
                        listren[i].t_KeHuZiYuanChis = null;
                        listren[i].t_RenYuanQuanXians = null;
                        listren[i].t_XiaoShouBiao_QueRenRens = null;
                        listren[i].t_XiaoShouBiao_YeWuYuans = null;
                        listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                        listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                        listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                        listren[i].t_XiaoXiTiXingBiaos = null;
                        listren[i].t_YiJianFanKuiBiaos = null;
                        listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                        listren[i].BeiZhu9 = "0";
                        var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                        foreach (var item in foreachlist)
                        {
                            if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                            {
                                if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                                {
                                    if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                    {
                                        listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                    }
                                    else
                                    {
                                        listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                    }
                                }
                            }
                        }
                        listren[i].BeiZhu10 = foreachlist.Count.ToString();
                    }
                    retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
                }
                else
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1));
                    var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                    var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
                    for (int i = 0; i < listren.Count; i++)
                    {
                        listren[i].t_BeiWangLus = null;
                        listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                        listren[i].t_KeHuBiaos = null;
                        listren[i].t_KeHuZiYuanChis = null;
                        listren[i].t_RenYuanQuanXians = null;
                        listren[i].t_XiaoShouBiao_QueRenRens = null;
                        listren[i].t_XiaoShouBiao_YeWuYuans = null;
                        listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                        listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                        listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                        listren[i].t_XiaoXiTiXingBiaos = null;
                        listren[i].t_YiJianFanKuiBiaos = null;
                        listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                        listren[i].BeiZhu9 = "0";
                        var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                        foreach (var item in foreachlist)
                        {
                            if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                            {
                                if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                                {
                                    if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                    {
                                        listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                    }
                                    else
                                    {
                                        listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                    }
                                }
                            }
                        }
                        listren[i].BeiZhu10 = foreachlist.Count.ToString();
                    }
                    retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
                }
            }
            for (int i = 0; i < retlist.Count; i++)
            {
                retlist[i].BeiZhu8 = (i + 1).ToString();
            }
            for (int i = 0; i < retlist.Count; i++)
            {
                retlist[i].t_GangWeiBiao = null;
                retlist[i].t_ZhanDuiBiao = null;
            }
            return Helper.ListToJsonAddFenYe(retlist, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
        }
        #endregion
        #region 团队排名
        [HttpPost]
        public string ReturnTeam()
        {
            List<RetClass> retClasses = new List<RetClass>();
            var retlist = new List<t_RenYuanBiao>();
            if (Request.Form["shijian"] == "yue")
            {
                var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                var shuju = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao != null).ToList();
                var listren = shuju.Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员").ToList();
                for (int i = 0; i < listren.Count; i++)
                {
                    listren[i].t_BeiWangLus = null;
                    listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                    listren[i].t_KeHuBiaos = null;
                    listren[i].t_KeHuZiYuanChis = null;
                    listren[i].t_RenYuanQuanXians = null;
                    listren[i].t_XiaoShouBiao_QueRenRens = null;
                    listren[i].t_XiaoShouBiao_YeWuYuans = null;
                    listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                    listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                    listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                    listren[i].t_XiaoXiTiXingBiaos = null;
                    listren[i].t_YiJianFanKuiBiaos = null;
                    listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                    var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                    listren[i].BeiZhu9 = "0";
                    foreach (var item in foreachlist)
                    {
                        if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                        {
                            if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                            {
                                if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                {
                                    listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                }
                                else
                                {
                                    listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                }
                            }
                        }
                    }
                    listren[i].BeiZhu10 = foreachlist.Count.ToString();
                }
                retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
            }
            else
            {
                var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
                var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1));
                var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员" || x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
                for (int i = 0; i < listren.Count; i++)
                {
                    listren[i].t_BeiWangLus = null;
                    listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                    listren[i].t_KeHuBiaos = null;
                    listren[i].t_KeHuZiYuanChis = null;
                    listren[i].t_RenYuanQuanXians = null;
                    listren[i].t_XiaoShouBiao_QueRenRens = null;
                    listren[i].t_XiaoShouBiao_YeWuYuans = null;
                    listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                    listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                    listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                    listren[i].t_XiaoXiTiXingBiaos = null;
                    listren[i].t_YiJianFanKuiBiaos = null;
                    listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                    listren[i].BeiZhu9 = "0";
                    var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                    foreach (var item in foreachlist)
                    {
                        if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                        {
                            if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                            {
                                if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                {
                                    listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                }
                                else
                                {
                                    listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                }
                            }
                        }
                    }
                    listren[i].BeiZhu10 = foreachlist.Count.ToString();
                }
                retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
            }
            for (int i = 0; i < retlist.Count; i++)
            {
                retlist[i].BeiZhu8 = (i + 1).ToString();
            }
            if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
            {
                var ZhanQuList = t_ZhanQuBiaoService.GetList();
                for (int i = 0; i < ZhanQuList.Count; i++)
                {
                    var listrenbybumen = retlist.Where(x => x.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ZhanQuList[i].Id).ToList();
                    RetClass retClass = new RetClass
                    {
                        Id = ZhanQuList[i].Id,
                        Name = ZhanQuList[i].ZhanQuMingCheng
                    };
                    foreach (var item in listrenbybumen)
                    {
                        retClass.DingDanShu += Convert.ToInt32(item.BeiZhu10);
                        retClass.JinE += Convert.ToInt32(item.BeiZhu9);
                    }
                    retClasses.Add(retClass);
                }
            }
            else
            {
                var listtuandui = t_TuanDuiBiaoService.GetList();
                for (int i = 0; i < listtuandui.Count; i++)
                {
                    RetClass retClass = new RetClass
                    {
                        Id = listtuandui[i].Id,
                        Name = listtuandui[i].TuanDuiMingCheng,
                        PeopleName = listtuandui[i].t_RenYuanBiao_JingLi.XingMing
                    };
                    foreach (var item in listtuandui[i].t_TuanDuiRenYuanGuanLianBiaos)
                    {
                        var sadf = retlist.Where(x => x.Id == item.t_RenYuanBiao.Id).ToList();
                        if (sadf.Count > 0)
                        {
                            retClass.DingDanShu += Convert.ToInt32(sadf[0].BeiZhu10);
                            retClass.JinE += Convert.ToInt32(sadf[0].BeiZhu9);
                        }
                    }
                    retClasses.Add(retClass);
                }
            }
            for (int i = 0; i < 2; i++)
            {
                if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu" && retClasses.Count > 0)
                {
                    retClasses.Remove(retClasses[i]);
                }
            }
            retClasses = retClasses.OrderByDescending(x => Convert.ToInt32(x.JinE)).ToList();
            for (int i = 0; i < retClasses.Count; i++)
            {
                retClasses[i].PaiMing = i + 1;
            }
            return Helper.ListToJsonAddFenYe(retClasses, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
        }
        public string ReturnTeamNoTable()
        {
            List<RetClassToFenXiBaoBiaoFour> RetClassToFenXiBaoBiaoFours = new List<RetClassToFenXiBaoBiaoFour>();
            var xiaoshouxiangxilist = t_XiaoShouXiangXiBiaoService.GetList();
            var renyuanbiaolist = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao != null).ToList();
            var listtuandui = t_TuanDuiBiaoService.GetList();
            var ZhanQuList = t_ZhanQuBiaoService.GetList();
            var retlist = new List<t_RenYuanBiao>();
            var StartTimeMonth = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
            var StartTimeYear = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
            if (Request.Form["shijian"] == "yue")
            {
                for (int x = 0; x < DateTime.Now.Day; x++)
                {
                    List<RetClass> retClasses = new List<RetClass>();
                    var StartTime = StartTimeMonth.AddDays(x);
                    var EndTime = StartTimeMonth.AddDays(x + 1);
                    var listxiaoshouxiangxi = xiaoshouxiangxilist.Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                    var listren = renyuanbiaolist.Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员").ToList();
                    for (int i = 0; i < listren.Count; i++)
                    {
                        listren[i].t_BeiWangLus = null;
                        listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                        listren[i].t_KeHuBiaos = null;
                        listren[i].t_KeHuZiYuanChis = null;
                        listren[i].t_RenYuanQuanXians = null;
                        listren[i].t_XiaoShouBiao_QueRenRens = null;
                        listren[i].t_XiaoShouBiao_YeWuYuans = null;
                        listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                        listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                        listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                        listren[i].t_XiaoXiTiXingBiaos = null;
                        listren[i].t_YiJianFanKuiBiaos = null;
                        listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                        var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                        listren[i].BeiZhu9 = "0";
                        foreach (var item in foreachlist)
                        {
                            if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                            {
                                if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                                {
                                    if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                    {
                                        listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                    }
                                    else
                                    {
                                        listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                    }
                                }
                            }
                        }
                        listren[i].BeiZhu10 = foreachlist.Count.ToString();
                    }
                    retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
                    if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
                    {
                        for (int i = 0; i < ZhanQuList.Count; i++)
                        {
                            var listrenbybumen = retlist.Where(x => x.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ZhanQuList[i].Id).ToList();
                            RetClass retClass = new RetClass
                            {
                                Id = ZhanQuList[i].Id,
                                Name = ZhanQuList[i].ZhanQuMingCheng
                            };
                            foreach (var item in listrenbybumen)
                            {
                                retClass.DingDanShu += Convert.ToInt32(item.BeiZhu10);
                                retClass.JinE += Convert.ToInt32(item.BeiZhu9);
                            }
                            retClasses.Add(retClass);
                        }
                    }
                    else
                    {
                        for (int i = 0; i < listtuandui.Count; i++)
                        {
                            RetClass retClass = new RetClass
                            {
                                Id = listtuandui[i].Id,
                                Name = listtuandui[i].TuanDuiMingCheng,
                                PeopleName = listtuandui[i].t_RenYuanBiao_JingLi.XingMing
                            };
                            if (listtuandui[i].t_TuanDuiRenYuanGuanLianBiaos != null)
                            {
                                foreach (var item in listtuandui[i].t_TuanDuiRenYuanGuanLianBiaos)
                                {
                                    var sadf = retlist.Where(x => x.Id == item.t_RenYuanBiao.Id).ToList();
                                    if (sadf.Count > 0)
                                    {
                                        retClass.DingDanShu += Convert.ToInt32(sadf[0].BeiZhu10);
                                        retClass.JinE += Convert.ToInt32(sadf[0].BeiZhu9);
                                    }
                                }
                                retClasses.Add(retClass);
                            }
                        }
                    }
                    RetClassToFenXiBaoBiaoFours.Add(new RetClassToFenXiBaoBiaoFour { Day = StartTime.Day, RetClass = retClasses });
                }
            }
            else
            {
                List<RetClass> retClasses = new List<RetClass>();
                var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
                var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1));
                var listxiaoshouxiangxi = xiaoshouxiangxilist.Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
                var listren = renyuanbiaolist.Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员" || x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
                for (int i = 0; i < listren.Count; i++)
                {
                    listren[i].t_BeiWangLus = null;
                    listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                    listren[i].t_KeHuBiaos = null;
                    listren[i].t_KeHuZiYuanChis = null;
                    listren[i].t_RenYuanQuanXians = null;
                    listren[i].t_XiaoShouBiao_QueRenRens = null;
                    listren[i].t_XiaoShouBiao_YeWuYuans = null;
                    listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                    listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                    listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                    listren[i].t_XiaoXiTiXingBiaos = null;
                    listren[i].t_YiJianFanKuiBiaos = null;
                    listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                    listren[i].BeiZhu9 = "0";
                    var foreachlist = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList();
                    foreach (var item in foreachlist)
                    {
                        if (item.t_XiaoShouBiao.ChengJiaoJinE != "" && item.t_XiaoShouBiao.ChengJiaoJinE != null)
                        {
                            if (Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE) > 0)
                            {
                                if (listren[i].BeiZhu9 == "" || listren[i].BeiZhu9 == null)
                                {
                                    listren[i].BeiZhu9 = item.t_XiaoShouBiao.ChengJiaoJinE;
                                }
                                else
                                {
                                    listren[i].BeiZhu9 = (Convert.ToInt32(listren[i].BeiZhu9) + Convert.ToInt32(item.t_XiaoShouBiao.ChengJiaoJinE)).ToString();
                                }
                            }
                        }
                    }
                    listren[i].BeiZhu10 = foreachlist.Count.ToString();
                }
                retlist = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu9)).ToList();
                if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
                {
                    //var ZhanQuList = t_ZhanQuBiaoService.GetList();
                    for (int i = 0; i < ZhanQuList.Count; i++)
                    {
                        var listrenbybumen = retlist.Where(x => x.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ZhanQuList[i].Id).ToList();
                        RetClass retClass = new RetClass
                        {
                            Id = ZhanQuList[i].Id,
                            Name = ZhanQuList[i].ZhanQuMingCheng
                        };
                        foreach (var item in listrenbybumen)
                        {
                            retClass.DingDanShu += Convert.ToInt32(item.BeiZhu10);
                            retClass.JinE += Convert.ToInt32(item.BeiZhu9);
                        }
                        retClasses.Add(retClass);
                    }
                }
                else
                {
                    //var listtuandui = t_TuanDuiBiaoService.GetList();
                    for (int i = 0; i < listtuandui.Count; i++)
                    {
                        RetClass retClass = new RetClass
                        {
                            Id = listtuandui[i].Id,
                            Name = listtuandui[i].TuanDuiMingCheng,
                            PeopleName = listtuandui[i].t_RenYuanBiao_JingLi.XingMing
                        };
                        foreach (var item in listtuandui[i].t_TuanDuiRenYuanGuanLianBiaos)
                        {
                            var sadf = retlist.Where(x => x.Id == item.t_RenYuanBiao.Id).ToList();
                            if (sadf.Count > 0)
                            {
                                retClass.DingDanShu += Convert.ToInt32(sadf[0].BeiZhu10);
                                retClass.JinE += Convert.ToInt32(sadf[0].BeiZhu9);
                            }
                        }
                        retClasses.Add(retClass);
                    }
                }
                RetClassToFenXiBaoBiaoFours.Add(new RetClassToFenXiBaoBiaoFour { Day = StartTime.Day, RetClass = retClasses });
            }
            return JsonConvert.SerializeObject(RetClassToFenXiBaoBiaoFours, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region 测试
        [HttpPost]
        public string ReturnXiaoShouETest()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            var retlist = new List<t_RenYuanBiao>();
            if (Request.Form["leixing"] == "yewuyuan")
            {
                if (Request.Form["shijian"] == "yue")
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                    List<t_RenYuanBiao> ListRenYuan = new List<t_RenYuanBiao>();
                    List<t_FangYuanKeHuGuanLianBiao> ListXiaoShous = new List<t_FangYuanKeHuGuanLianBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListRen = _context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员").ToList();
                        var xiaoshou = _context.t_XiaoShouBiao.Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").ToList();
                        ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        xiaoshou = xiaoshou.Where(x => Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                        for (int i = 0; i < xiaoshou.Count; i++)
                        {
                            if (ListRenYuan.Where(x => x.Id == xiaoshou[i].YeWuYuanId).ToList().Count > 0)
                            {
                                for (int j = 0; j < ListRenYuan.Count; j++)
                                {
                                    if (ListRenYuan[j].Id == xiaoshou[i].YeWuYuanId)
                                    {
                                        ListRenYuan[j].BeiZhu9 = (BigInteger.Parse(ListRenYuan[j].BeiZhu9) + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id).ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString())).ToString();
                                        ListRenYuan[j].t_XiaoShouBiao_YeWuYuans.Add(xiaoshou[i]);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                var IptPeople = ListRen.Where(x => x.Id == xiaoshou[i].YeWuYuanId).FirstOrDefault();
                                if (IptPeople != null)
                                {
                                    if (IptPeople.t_GangWeiBiao != null)
                                    {
                                        IptPeople.t_GangWeiBiao = null;
                                    }
                                    if (xiaoshou[i].t_RenYuanBiao_YeWuYuan != null)
                                    {
                                        xiaoshou[i].t_RenYuanBiao_YeWuYuan = null;
                                    }
                                    List<t_XiaoShouBiao> list = new List<t_XiaoShouBiao>() { xiaoshou[i] };
                                    IptPeople.t_XiaoShouBiao_YeWuYuans = list;
                                    IptPeople.BeiZhu9 = xiaoshou[i].ChengJiaoJinE;
                                    ListRenYuan.Add(IptPeople);
                                }
                            }
                        }
                    }
                    ListRenYuan = ListRenYuan.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < ListRenYuan.Count; i++)
                    {
                        for (int j = 0; j < ListRenYuan[i].t_XiaoShouBiao_YeWuYuans.Count; j++)
                        {
                            ListRenYuan[i].BeiZhu10 = (Convert.ToInt32(ListRenYuan[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListRenYuan[i].t_XiaoShouBiao_YeWuYuans[j].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                        }
                        ListRenYuan[i].BeiZhu8 = (i + 1).ToString();
                        ListRenYuan[i].t_XiaoShouBiao_YeWuYuans = null;
                    }
                    return Helper.ListToJsonAddFenYe(ListRenYuan, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
                }
                else
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1));
                    List<t_RenYuanBiao> ListRenYuan = new List<t_RenYuanBiao>();
                    List<t_FangYuanKeHuGuanLianBiao> ListXiaoShous = new List<t_FangYuanKeHuGuanLianBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListRen = _context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员").ToList();
                        var xiaoshou = _context.t_XiaoShouBiao.Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").ToList();
                        ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        xiaoshou = xiaoshou.Where(x => Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                        for (int i = 0; i < xiaoshou.Count; i++)
                        {
                            if (ListRenYuan.Where(x => x.Id == xiaoshou[i].YeWuYuanId).ToList().Count > 0)
                            {
                                for (int j = 0; j < ListRenYuan.Count; j++)
                                {
                                    if (ListRenYuan[j].Id == xiaoshou[i].YeWuYuanId)
                                    {
                                        ListRenYuan[j].BeiZhu9 = (BigInteger.Parse(ListRenYuan[j].BeiZhu9) + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id).ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString())).ToString();
                                        ListRenYuan[j].t_XiaoShouBiao_YeWuYuans.Add(xiaoshou[i]);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                var IptPeople = ListRen.Where(x => x.Id == xiaoshou[i].YeWuYuanId).FirstOrDefault();
                                if (IptPeople != null)
                                {
                                    if (IptPeople.t_GangWeiBiao != null)
                                    {
                                        IptPeople.t_GangWeiBiao = null;
                                    }
                                    if (xiaoshou[i].t_RenYuanBiao_YeWuYuan != null)
                                    {
                                        xiaoshou[i].t_RenYuanBiao_YeWuYuan = null;
                                    }
                                    List<t_XiaoShouBiao> list = new List<t_XiaoShouBiao>() { xiaoshou[i] };
                                    IptPeople.t_XiaoShouBiao_YeWuYuans = list;
                                    IptPeople.BeiZhu9 = xiaoshou[i].ChengJiaoJinE;
                                    ListRenYuan.Add(IptPeople);
                                }
                            }
                        }
                    }
                    ListRenYuan = ListRenYuan.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < ListRenYuan.Count; i++)
                    {
                        for (int j = 0; j < ListRenYuan[i].t_XiaoShouBiao_YeWuYuans.Count; j++)
                        {
                            ListRenYuan[i].BeiZhu10 = (Convert.ToInt32(ListRenYuan[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListRenYuan[i].t_XiaoShouBiao_YeWuYuans[j].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                        }
                        ListRenYuan[i].BeiZhu8 = (i + 1).ToString();
                        ListRenYuan[i].t_XiaoShouBiao_YeWuYuans = null;
                    }
                    return Helper.ListToJsonAddFenYe(ListRenYuan, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
                }
            }
            else
            {
                if (Request.Form["shijian"] == "yue")
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                    List<t_RenYuanBiao> ListRenYuan = new List<t_RenYuanBiao>();
                    List<t_FangYuanKeHuGuanLianBiao> ListXiaoShous = new List<t_FangYuanKeHuGuanLianBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListRen = _context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
                        var xiaoshou = _context.t_XiaoShouBiao.Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").ToList();
                        ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        xiaoshou = xiaoshou.Where(x => Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                        for (int i = 0; i < xiaoshou.Count; i++)
                        {
                            if (ListRenYuan.Where(x => x.Id == xiaoshou[i].ZhiYeGuWenId).ToList().Count > 0)
                            {
                                for (int j = 0; j < ListRenYuan.Count; j++)
                                {
                                    if (ListRenYuan[j].Id == xiaoshou[i].ZhiYeGuWenId)
                                    {
                                        ListRenYuan[j].BeiZhu9 = (BigInteger.Parse(ListRenYuan[j].BeiZhu9) + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id).ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString())).ToString();
                                        ListRenYuan[j].t_XiaoShouBiao_YeWuYuans.Add(xiaoshou[i]);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                var IptPeople = ListRen.Where(x => x.Id == xiaoshou[i].ZhiYeGuWenId).FirstOrDefault();
                                if (IptPeople != null)
                                {
                                    if (IptPeople.t_GangWeiBiao != null)
                                    {
                                        IptPeople.t_GangWeiBiao = null;
                                    }
                                    if (xiaoshou[i].t_RenYuanBiao_YeWuYuan != null)
                                    {
                                        xiaoshou[i].t_RenYuanBiao_YeWuYuan = null;
                                    }
                                    if (xiaoshou[i].t_RenYuanBiao_ZhiYeGuWen != null)
                                    {
                                        xiaoshou[i].t_RenYuanBiao_ZhiYeGuWen = null;
                                    }
                                    List<t_XiaoShouBiao> list = new List<t_XiaoShouBiao>() { xiaoshou[i] };
                                    IptPeople.t_XiaoShouBiao_YeWuYuans = list;
                                    IptPeople.BeiZhu9 = xiaoshou[i].ChengJiaoJinE;
                                    ListRenYuan.Add(IptPeople);
                                }
                            }
                        }
                    }
                    ListRenYuan = ListRenYuan.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < ListRenYuan.Count; i++)
                    {
                        for (int j = 0; j < ListRenYuan[i].t_XiaoShouBiao_YeWuYuans.Count; j++)
                        {
                            ListRenYuan[i].BeiZhu10 = (Convert.ToInt32(ListRenYuan[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListRenYuan[i].t_XiaoShouBiao_YeWuYuans[j].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                        }
                        ListRenYuan[i].BeiZhu8 = (i + 1).ToString();
                        ListRenYuan[i].t_XiaoShouBiao_YeWuYuans = null;
                    }
                    return Helper.ListToJsonAddFenYe(ListRenYuan, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
                }
                else
                {
                    var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                    var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                    List<t_RenYuanBiao> ListRenYuan = new List<t_RenYuanBiao>();
                    List<t_FangYuanKeHuGuanLianBiao> ListXiaoShous = new List<t_FangYuanKeHuGuanLianBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListRen = _context.t_RenYuanBiao.Include(x => x.t_GangWeiBiao).Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
                        var xiaoshou = _context.t_XiaoShouBiao.Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").ToList();
                        ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        xiaoshou = xiaoshou.Where(x => Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                        for (int i = 0; i < xiaoshou.Count; i++)
                        {
                            if (ListRenYuan.Where(x => x.Id == xiaoshou[i].ZhiYeGuWenId).ToList().Count > 0)
                            {
                                for (int j = 0; j < ListRenYuan.Count; j++)
                                {
                                    if (ListRenYuan[j].Id == xiaoshou[i].ZhiYeGuWenId)
                                    {
                                        ListRenYuan[j].BeiZhu9 = (BigInteger.Parse(ListRenYuan[j].BeiZhu9) + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == xiaoshou[i].Id).ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString())).ToString();
                                        ListRenYuan[j].t_XiaoShouBiao_YeWuYuans.Add(xiaoshou[i]);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                var IptPeople = ListRen.Where(x => x.Id == xiaoshou[i].ZhiYeGuWenId).FirstOrDefault();
                                if (IptPeople != null)
                                {
                                    if (IptPeople.t_GangWeiBiao != null)
                                    {
                                        IptPeople.t_GangWeiBiao = null;
                                    }
                                    if (xiaoshou[i].t_RenYuanBiao_YeWuYuan != null)
                                    {
                                        xiaoshou[i].t_RenYuanBiao_YeWuYuan = null;
                                    }
                                    if (xiaoshou[i].t_RenYuanBiao_ZhiYeGuWen != null)
                                    {
                                        xiaoshou[i].t_RenYuanBiao_ZhiYeGuWen = null;
                                    }
                                    List<t_XiaoShouBiao> list = new List<t_XiaoShouBiao>() { xiaoshou[i] };
                                    IptPeople.t_XiaoShouBiao_YeWuYuans = list;
                                    IptPeople.BeiZhu9 = xiaoshou[i].ChengJiaoJinE;
                                    ListRenYuan.Add(IptPeople);
                                }
                            }
                        }
                    }
                    ListRenYuan = ListRenYuan.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < ListRenYuan.Count; i++)
                    {
                        for (int j = 0; j < ListRenYuan[i].t_XiaoShouBiao_YeWuYuans.Count; j++)
                        {
                            ListRenYuan[i].BeiZhu10 = (Convert.ToInt32(ListRenYuan[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListRenYuan[i].t_XiaoShouBiao_YeWuYuans[j].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                        }
                        ListRenYuan[i].BeiZhu8 = (i + 1).ToString();
                        ListRenYuan[i].t_XiaoShouBiao_YeWuYuans = null;
                    }
                    return Helper.ListToJsonAddFenYe(ListRenYuan, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
                }
            }
        }
        [HttpPost]
        public string ReturnTeamTest()
        {
            List<RetClass> retClasses = new List<RetClass>();
            List<t_RenYuanBiao> ListRenYuan = new List<t_RenYuanBiao>();
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            if (Request.Form["shijian"] == "yue")
            {
                var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
                var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00").AddMonths(1));
                if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
                {
                    List<t_ZhanQuBiao> t_ZhanQuBiaos = new List<t_ZhanQuBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListXiaoShou = _context.t_XiaoShouBiao.Include(x => x.t_RenYuanBiao_YeWuYuan).ThenInclude(x => x.t_ZhanDuiBiao).ThenInclude(x => x.t_ZhanQuBiao).Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").AsNoTracking().ToList();
                        var ListZhanQu = _context.t_ZhanQuBiao.ToList();
                        var ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        for (int i = 0; i < ListZhanQu.Count; i++)
                        {
                            var ListXiaoShouAfter = ListXiaoShou.Where(x => x.t_RenYuanBiao_YeWuYuan.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ListZhanQu[i].Id && Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                            //ListZhanQu[i].BeiZhu10 = ListXiaoShouAfter.Count.ToString();
                            for (int t = 0; t < ListXiaoShouAfter.Count; t++)
                            {
                                if (ListZhanQu[i].BeiZhu10 == null || ListZhanQu[i].BeiZhu10 == "")
                                {
                                    ListZhanQu[i].BeiZhu10 = "0";
                                }
                                ListZhanQu[i].BeiZhu10 = (Convert.ToInt32(ListZhanQu[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListXiaoShouAfter[t].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                            }
                            BigInteger bigInteger = 0;
                            foreach (var item in ListXiaoShouAfter)
                            {
                                bigInteger = bigInteger + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == item.Id && x.BeiZhu1 != "已退房").ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString());
                            }
                            ListZhanQu[i].BeiZhu9 = bigInteger.ToString();
                        }
                        t_ZhanQuBiaos = ListZhanQu;
                    }
                    t_ZhanQuBiaos = t_ZhanQuBiaos.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < t_ZhanQuBiaos.Count; i++)
                    {
                        t_ZhanQuBiaos[i].BeiZhu8 = (i + 1).ToString();
                        RetClass retClass = new RetClass
                        {
                            Name = t_ZhanQuBiaos[i].ZhanQuMingCheng,
                            DingDanShu = Convert.ToInt32(t_ZhanQuBiaos[i].BeiZhu10),
                            JinEBig = t_ZhanQuBiaos[i].BeiZhu9,
                            PaiMing = Convert.ToInt32(t_ZhanQuBiaos[i].BeiZhu8)
                        };
                        retClasses.Add(retClass);
                    }
                }
                else
                {
                    List<FangChanCrmModel.t_TuanDuiBiao> t_TuanDuiBiaos = new List<FangChanCrmModel.t_TuanDuiBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListXiaoShou = _context.t_XiaoShouBiao.Include(x => x.t_RenYuanBiao_ZhiYeGuWen).ThenInclude(x => x.t_TuanDuiRenYuanGuanLianBiaos).Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").AsNoTracking().ToList();
                        var ListTuanDui = _context.t_TuanDuiBiao.ToList();
                        var ListZhanQu = _context.t_ZhanQuBiao.ToList();
                        var ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        for (int i = 0; i < ListTuanDui.Count; i++)
                        {
                            //var ListXiaoShouAfter = ListXiaoShou.Where(x => x.t_RenYuanBiao_ZhiYeGuWen.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ListZhanQu[i].Id && Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                            var ListXiaoShouAfter = ListXiaoShou.Where(x => x.t_RenYuanBiao_ZhiYeGuWen.t_TuanDuiRenYuanGuanLianBiaos[0].TuanDuiId == ListTuanDui[i].Id && Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                            for (int t = 0; t < ListXiaoShouAfter.Count; t++)
                            {
                                if (ListTuanDui[i].BeiZhu10 == null || ListTuanDui[i].BeiZhu10 == "")
                                {
                                    ListTuanDui[i].BeiZhu10 = "0";
                                }
                                ListTuanDui[i].BeiZhu10 = (Convert.ToInt32(ListTuanDui[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListXiaoShouAfter[t].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                            }
                            BigInteger bigInteger = 0;
                            foreach (var item in ListXiaoShouAfter)
                            {
                                //ListZhanQu[i].BeiZhu9 = (BigInteger.Parse(ListZhanQu[i].BeiZhu9) + BigInteger.Parse(item.ChengJiaoJinE));
                                bigInteger = bigInteger + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == item.Id && x.BeiZhu1 != "已退房").ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString());
                            }
                            ListTuanDui[i].BeiZhu9 = bigInteger.ToString();
                        }
                        t_TuanDuiBiaos = ListTuanDui;
                    }
                    t_TuanDuiBiaos = t_TuanDuiBiaos.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < t_TuanDuiBiaos.Count; i++)
                    {
                        t_TuanDuiBiaos[i].BeiZhu8 = (i + 1).ToString();
                        RetClass retClass = new RetClass
                        {
                            Name = t_TuanDuiBiaos[i].TuanDuiMingCheng,
                            DingDanShu = Convert.ToInt32(t_TuanDuiBiaos[i].BeiZhu10),
                            JinEBig = t_TuanDuiBiaos[i].BeiZhu9,
                            PaiMing = Convert.ToInt32(t_TuanDuiBiaos[i].BeiZhu8)
                        };
                        retClasses.Add(retClass);
                    }
                }
            }
            else
            {
                var StartTime = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
                var EndTime = Convert.ToDateTime(Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00").AddYears(1));
                if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
                {
                    List<t_ZhanQuBiao> t_ZhanQuBiaos = new List<t_ZhanQuBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListXiaoShou = _context.t_XiaoShouBiao.Include(x => x.t_RenYuanBiao_YeWuYuan).ThenInclude(x => x.t_ZhanDuiBiao).ThenInclude(x => x.t_ZhanQuBiao).Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").AsNoTracking().ToList();
                        var ListZhanQu = _context.t_ZhanQuBiao.ToList();
                        var ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        for (int i = 0; i < ListZhanQu.Count; i++)
                        {
                            var ListXiaoShouAfter = ListXiaoShou.Where(x => x.t_RenYuanBiao_YeWuYuan.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ListZhanQu[i].Id && Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                            //ListZhanQu[i].BeiZhu10 = ListXiaoShouAfter.Count.ToString();
                            for (int t = 0; t < ListXiaoShouAfter.Count; t++)
                            {
                                if (ListZhanQu[i].BeiZhu10 == null || ListZhanQu[i].BeiZhu10 == "")
                                {
                                    ListZhanQu[i].BeiZhu10 = "0";
                                }
                                ListZhanQu[i].BeiZhu10 = (Convert.ToInt32(ListZhanQu[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListXiaoShouAfter[t].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                            }
                            BigInteger bigInteger = 0;
                            foreach (var item in ListXiaoShouAfter)
                            {
                                //ListZhanQu[i].BeiZhu9 = (BigInteger.Parse(ListZhanQu[i].BeiZhu9) + BigInteger.Parse(item.ChengJiaoJinE));
                                bigInteger = bigInteger + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == item.Id && x.BeiZhu1 != "已退房").ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString());
                            }
                            ListZhanQu[i].BeiZhu9 = bigInteger.ToString();
                        }
                        t_ZhanQuBiaos = ListZhanQu;
                    }
                    t_ZhanQuBiaos = t_ZhanQuBiaos.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < t_ZhanQuBiaos.Count; i++)
                    {
                        t_ZhanQuBiaos[i].BeiZhu8 = (i + 1).ToString();
                        RetClass retClass = new RetClass
                        {
                            Name = t_ZhanQuBiaos[i].ZhanQuMingCheng,
                            DingDanShu = Convert.ToInt32(t_ZhanQuBiaos[i].BeiZhu10),
                            JinEBig = t_ZhanQuBiaos[i].BeiZhu9,
                            PaiMing = Convert.ToInt32(t_ZhanQuBiaos[i].BeiZhu8)
                        };
                        retClasses.Add(retClass);
                    }
                }
                else
                {
                    List<FangChanCrmModel.t_TuanDuiBiao> t_TuanDuiBiaos = new List<FangChanCrmModel.t_TuanDuiBiao>();
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var ListXiaoShou = _context.t_XiaoShouBiao.Include(x => x.t_RenYuanBiao_ZhiYeGuWen).ThenInclude(x => x.t_TuanDuiRenYuanGuanLianBiaos).Where(x => x.BeiZhu1 != null && x.BeiZhu1 != "").AsNoTracking().ToList();
                        var ListTuanDui = _context.t_TuanDuiBiao.ToList();
                        var ListXiaoShous = _context.t_FangYuanKeHuGuanLianBiao.ToList();
                        for (int i = 0; i < ListTuanDui.Count; i++)
                        {
                            //var ListXiaoShouAfter = ListXiaoShou.Where(x => x.t_RenYuanBiao_ZhiYeGuWen.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ListZhanQu[i].Id && Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                            var ListXiaoShouAfter = ListXiaoShou.Where(x => x.t_RenYuanBiao_ZhiYeGuWen.t_TuanDuiRenYuanGuanLianBiaos[0].TuanDuiId == ListTuanDui[i].Id && Convert.ToDateTime(x.BeiZhu1) > StartTime && Convert.ToDateTime(x.BeiZhu1) < EndTime).ToList();
                            //ListTuanDui[i].BeiZhu10 = ListXiaoShouAfter.Count.ToString();
                            for (int t = 0; t < ListXiaoShouAfter.Count; t++)
                            {
                                if (ListTuanDui[i].BeiZhu10 == null || ListTuanDui[i].BeiZhu10 == "")
                                {
                                    ListTuanDui[i].BeiZhu10 = "0";
                                }
                                ListTuanDui[i].BeiZhu10 = (Convert.ToInt32(ListTuanDui[i].BeiZhu10) + Convert.ToInt32(ListXiaoShous.Where(x => x.XiaoShouBiaoId == ListXiaoShouAfter[t].Id && x.BeiZhu1 != "已退房").ToList().Count)).ToString();
                            }
                            BigInteger bigInteger = 0;
                            foreach (var item in ListXiaoShouAfter)
                            {
                                //ListZhanQu[i].BeiZhu9 = (BigInteger.Parse(ListZhanQu[i].BeiZhu9) + BigInteger.Parse(item.ChengJiaoJinE));
                                bigInteger = bigInteger + BigInteger.Parse(ListXiaoShous.Where(x => x.XiaoShouBiaoId == item.Id && x.BeiZhu1 != "已退房").ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE)).ToString());
                            }
                            ListTuanDui[i].BeiZhu9 = bigInteger.ToString();
                        }
                        t_TuanDuiBiaos = ListTuanDui;
                    }
                    t_TuanDuiBiaos = t_TuanDuiBiaos.OrderByDescending(x => BigInteger.Parse(x.BeiZhu9)).ToList();
                    for (int i = 0; i < t_TuanDuiBiaos.Count; i++)
                    {
                        t_TuanDuiBiaos[i].BeiZhu8 = (i + 1).ToString();
                        RetClass retClass = new RetClass
                        {
                            Name = t_TuanDuiBiaos[i].TuanDuiMingCheng,
                            DingDanShu = Convert.ToInt32(t_TuanDuiBiaos[i].BeiZhu10),
                            JinEBig = t_TuanDuiBiaos[i].BeiZhu9,
                            PaiMing = Convert.ToInt32(t_TuanDuiBiaos[i].BeiZhu8)
                        };
                        retClasses.Add(retClass);
                    }
                }
            }
            return Helper.ListToJsonAddFenYe(retClasses, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
        }
        [HttpPost]
        public string ReturnTeamNoTableTest()
        {
            List<RetClassToFenXiBaoBiaoFour> RetClassToFenXiBaoBiaoFours = new List<RetClassToFenXiBaoBiaoFour>();
            var StartTimeMonth = Convert.ToDateTime(DateTime.Now.Year + "/" + DateTime.Now.Month + "/01 00:00:00");
            var StartTimeYear = Convert.ToDateTime(DateTime.Now.Year + "/01/01 00:00:00");
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            if (Request.Form["shijian"] == "yue")
            {
                if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
                {
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var xiaoshouList = _context.t_XiaoShouBiao.Include(x => x.t_XiaoShouXiangXiBiaos).Include(x => x.t_RenYuanBiao_YeWuYuan).ThenInclude(x => x.t_ZhanDuiBiao).ThenInclude(x => x.t_ZhanQuBiao).ToList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeMonth && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeMonth.AddMonths(1) && x.t_XiaoShouXiangXiBiaos.Where(y => y.XiaoShouDongZuo == "已认筹" || y.XiaoShouDongZuo == "已认购").ToList().Count > 0).ToList();
                        var zhanquList = _context.t_ZhanQuBiao.ToList();
                        for (int i = 0; i < DateTime.Now.Day; i++)
                        {
                            var Reclasses = new List<RetClass>();
                            var xiaoshouListAfter = xiaoshouList.Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeMonth.AddDays(i) && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeMonth.AddDays(i + 1)).ToList();
                            for (int j = 0; j < zhanquList.Count; j++)
                            {
                                var xiaoshouListAfterZhanQu = xiaoshouListAfter.Where(x => x.t_RenYuanBiao_YeWuYuan.t_ZhanDuiBiao.t_ZhanQuBiao.Id == zhanquList[j].Id).ToList();
                                Reclasses.Add(new RetClass
                                {
                                    Name = zhanquList[j].ZhanQuMingCheng,
                                    Id = zhanquList[j].Id,
                                    DingDanShu = xiaoshouListAfterZhanQu.Count
                                });
                            }
                            RetClassToFenXiBaoBiaoFours.Add(new RetClassToFenXiBaoBiaoFour { Day = i + 1, RetClass = Reclasses });
                        }
                    }
                }
                else
                {
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var tuanduiList = _context.t_TuanDuiBiao.ToList();
                        var xiaoshouList = _context.t_XiaoShouBiao.Include(x => x.t_XiaoShouXiangXiBiaos).Include(x => x.t_RenYuanBiao_ZhiYeGuWen).ThenInclude(x => x.t_TuanDuiRenYuanGuanLianBiaos).ToList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeMonth && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeMonth.AddDays(1) && x.t_XiaoShouXiangXiBiaos.Where(y => y.XiaoShouDongZuo == "已认筹" || y.XiaoShouDongZuo == "已认购").ToList().Count > 0).ToList();
                        for (int i = 0; i < DateTime.Now.Day; i++)
                        {
                            var Reclasses = new List<RetClass>();
                            for (int j = 0; j < tuanduiList.Count; j++)
                            {
                                var xiaoshouListAfter = xiaoshouList.Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeMonth.AddDays(i) && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeMonth.AddDays(i + 1) && x.t_RenYuanBiao_ZhiYeGuWen.t_TuanDuiRenYuanGuanLianBiaos.Where(y => y.Id == tuanduiList[j].Id).ToList().Count > 0).ToList();
                                Reclasses.Add(new RetClass
                                {
                                    Name = tuanduiList[j].TuanDuiMingCheng,
                                    Id = tuanduiList[j].Id,
                                    DingDanShu = xiaoshouListAfter.Count
                                });
                            }
                            RetClassToFenXiBaoBiaoFours.Add(new RetClassToFenXiBaoBiaoFour { Day = i + 1, RetClass = Reclasses });
                        }
                    }
                }

            }
            else
            {
                if (Request.Form["leixing"].ToString() == "xiaoshouzhanqu")
                {
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var xiaoshouList = _context.t_XiaoShouBiao.Include(x => x.t_XiaoShouXiangXiBiaos).Include(x => x.t_RenYuanBiao_YeWuYuan).ThenInclude(x => x.t_ZhanDuiBiao).ThenInclude(x => x.t_ZhanQuBiao).ToList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeYear && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeYear.AddYears(1) && x.t_XiaoShouXiangXiBiaos.Where(y => y.XiaoShouDongZuo == "已认筹" || y.XiaoShouDongZuo == "已认购").ToList().Count > 0).ToList();
                        var zhanquList = _context.t_ZhanQuBiao.ToList();
                        for (int i = 0; i < DateTime.Now.Month; i++)
                        {
                            var Reclasses = new List<RetClass>();
                            var xiaoshouListAfter = xiaoshouList.Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeYear.AddMonths(i) && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeYear.AddMonths(i + 1)).ToList();
                            for (int j = 0; j < zhanquList.Count; j++)
                            {
                                var xiaoshouListAfterZhanQu = xiaoshouListAfter.Where(x => x.t_RenYuanBiao_YeWuYuan.t_ZhanDuiBiao.t_ZhanQuBiao.Id == zhanquList[j].Id).ToList();
                                Reclasses.Add(new RetClass
                                {
                                    Name = zhanquList[j].ZhanQuMingCheng,
                                    Id = zhanquList[j].Id,
                                    DingDanShu = xiaoshouListAfterZhanQu.Count
                                });
                            }
                            RetClassToFenXiBaoBiaoFours.Add(new RetClassToFenXiBaoBiaoFour { Day = i + 1, RetClass = Reclasses });
                        }
                    }
                }
                else
                {
                    using (var _context = new MyContext(optionsBuilder.Options))
                    {
                        var tuanduiList = _context.t_TuanDuiBiao.ToList();
                        var xiaoshouList = _context.t_XiaoShouBiao.Include(x => x.t_XiaoShouXiangXiBiaos).Include(x => x.t_RenYuanBiao_ZhiYeGuWen).ThenInclude(x => x.t_TuanDuiRenYuanGuanLianBiaos).ToList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeYear && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeYear.AddYears(1) && x.t_XiaoShouXiangXiBiaos.Where(y => y.XiaoShouDongZuo == "已认筹" || y.XiaoShouDongZuo == "已认购").ToList().Count > 0).ToList();
                        for (int i = 0; i < DateTime.Now.Month; i++)
                        {
                            var Reclasses = new List<RetClass>();
                            for (int j = 0; j < tuanduiList.Count; j++)
                            {
                                var xiaoshouListAfter = xiaoshouList.Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTimeYear.AddMonths(i) && Convert.ToDateTime(x.ShengChengShiJian) < StartTimeYear.AddMonths(i + 1) && x.t_RenYuanBiao_ZhiYeGuWen.t_TuanDuiRenYuanGuanLianBiaos.Where(y => y.Id == tuanduiList[j].Id).ToList().Count > 0).ToList();
                                Reclasses.Add(new RetClass
                                {
                                    Name = tuanduiList[j].TuanDuiMingCheng,
                                    Id = tuanduiList[j].Id,
                                    DingDanShu = xiaoshouListAfter.Count
                                });
                            }
                            RetClassToFenXiBaoBiaoFours.Add(new RetClassToFenXiBaoBiaoFour { Day = i + 1, RetClass = Reclasses });
                        }
                    }
                }

            }
            return JsonConvert.SerializeObject(RetClassToFenXiBaoBiaoFours, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
    }
    public class RetClassToFenXiBaoBiaoFour
    {
        public int Day { get; set; }
        public List<RetClass> RetClass { get; set; }
    }
}
